import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.util.Hashtable;

public class RainDisplay extends JPanel implements ActionListener
{
	Rain rainmaker;
	private Timer textTime;
	private Thread rainThread;
	private int alphavalue = 0;
	Font font;
	JLabel text;
	public RainDisplay()
	{
		super();
		//Sets a black Background (duh)
		setBackground(Color.black);
		//Constructs a new Rain object with the same dimensions as the window
		rainmaker = new Rain(800, 600, (JPanel) this);
		//make a new thread with the Rain runnable as its argument
		rainThread = new Thread(rainmaker);

		
		//initializes textTime with a 3000 milisecond wait time, and creates a new actionlistener for it
		textTime = new Timer(3000, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//After 3 seconds elapses, the actionPerformed method draws the labels
				drawLabel();
			}
		});
		//This ensures the anonymous inner class's actionPerformed is only executed once
		textTime.setRepeats(false);
		//The 3 seconds starts ticking
		textTime.start();
		
	}
	public void actionPerformed(ActionEvent e)
	{
		paintComponent(getGraphics());
	}
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		//Checks if the rain-drawing thread has yet to be run
		if (rainThread.getState() == Thread.State.NEW) {
			//when the thread is started, rainmaker's run method is executed in that new thread
			rainThread.start();
		}

	}
	public void makeButtons()
	{
		//Creates two JButtons & adds them to this JPanel
		JButton yes = new JButton("Yes");
		JButton no = new JButton("No");
		
		yes.setBounds(190, 350, 120, 50);
		yes.setOpaque(false);
		yes.setFont(font);
		yes.setText("YES");
	 	yes.setVisible(true);
	 	//yes.setIgnoreRepaint(true);

		no.setBounds(500, 350, 120, 50);
		no.setOpaque(false);
		no.setFont(font);
		no.setText("NO");
		no.setVisible(true);
		//no.setIgnoreRepaint(true);
		
		//Could be this.add, but that would be unncessary
		add(no);
		add(yes);
	}
	public void drawLabel()
	{

				text = new JLabel("sadf");
				text.setBounds(70, 30, 800, 200);
				text.setText("DO YOU WANT TO PLAY A GAME?");
				text.setVisible(true);
				add(text);

		//Make a new thread to keep increasing the opacity of the text until it reaches max (255)
		Thread opacity = new Thread(new Runnable() {
			
			public void run() {
				while (alphavalue <= 255) {
					//This does some fancy Hashtable and font work that i'm not quite sure about
					Hashtable<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
					map.put(TextAttribute.FOREGROUND, new Color(0, 255, 0, alphavalue));
					map.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
					font = new Font("Monospaced", Font.PLAIN, 40);
					font = font.deriveFont(map);
				
					alphavalue += 5;
					//When the text is almost finished, draw the buttons
					/*if (alphavalue == 240)
						makeButtons();*/
					//redraw the text
					text.setFont(font);
					text.repaint();
					try {
						Thread.sleep(20);
					} catch (InterruptedException e) { }
				}
			}
		});
		opacity.start();

	}
}